************************************
************************************
**** Replication files for:
****
**** Democratizing US Courts: Perceived Representation and Support for 
**** Judicial Elections in Political Research Quarterly
**** by Miles T. Armaly and John M. Bruce
**** Analysis in Stata/SE 18.0
**** Figures in R
************************************
************************************
************************************

** read in data for diversity question/Figure 1
import delimited "anonymized_diversity items.csv", clear 
drop if distri == "test"
drop if distri == "preview"

** info presented in Figure 1
* racial/ethnic minorities
mean q7
* females
mean q6


*********************
*********************


** read in module for remaining analyses
use "CCES20_UMS_OUTPUT.DTA", clear

*merge with common content
merge m:m caseid using "/Users/Miles/Dropbox/Sex, Efficacy, & SCOTUS support/Data/CES20_Common_OUTPUT_vv.dta"
drop if _merge != 3


* racial attitudes (from common content)
replace CC20_440b = 6- CC20_440b
alpha CC20_440a CC20_440b, gen(racial)


* remaining variables from module *


* Legitimacy & politicization
foreach var of varlist UMS382 UMS384 UMS385{
	replace `var' = 6-`var'
}
*
alpha UMS382 UMS384 UMS385, gen(legitimacy)
alpha UMS386 UMS387, gen(politicization)

* Perceived Representation
gen UMS388b = 6-UMS388
gen UMS389b = 6-UMS389

alpha UMS388b-UMS389b, gen(representation)

* Political sexism -- high values == agree with sexist thing
gen sex1 = 6-CC20_440c
gen sex2 = 6-UMS392
gen sex3 = 6-UMS393
gen sex4 = UMS395
factor sex1-sex4, ipf
alpha sex1-sex4, gen(sexism)

* DV: support for judicial elections
gen elect = UMS397
gen electedBetter = 6-UMS398
alpha elect electedBetter, gen(dvElect)

* Supreme Court support
gen SCapproval = . 
replace SCapproval = 1 if CC20_320c == 4
replace SCapproval = 2 if CC20_320c == 3
replace SCapproval = 3 if CC20_320c == 2
replace SCapproval = 4 if CC20_320c == 1
 
* Trump support
gen presapproval = 6- CC20_320a 


* Affective polarization
gen affective = abs(UMS403-UMS402)


* Age
gen age = 2021-birthyr

* sophistication
gen interest = .
replace interest = 1 if newsint == 4
replace interest = 2 if newsint == 3
replace interest = 3 if newsint == 2
replace interest = 4 if newsint == 1

gen voting = .
replace voting = 0 if CC20_363 > 4 & CC20_363 < .
replace voting = 1 if CC20_363 < 5

gen horparty = .
replace horparty = 0 if CC20_310a == 1
replace horparty = 0 if CC20_310a == 3
replace horparty = 0 if CC20_310a == 4
replace horparty = 1 if CC20_310a == 2

factor interest voting horparty, ipf factor(1)
predict sophistication 


*Ideology
gen ideology = ideo5
replace ideology = . if ideology == 6
gen ideosquared = ideology*ideology

*Partisanship
gen partyid = pid7
replace partyid = . if partyid == 8
gen rep = . 
replace rep = 0 if partyid < 4
replace rep = 1 if partyid > 4 & partyid < .


* Race
gen white = 0
replace white = 1 if race == 1

gen black = 0
replace black = 1 if race == 2

gen latinx = 0
replace latinx = race == 3


** rescaling all variables 0-1
foreach var of varlist sophistication rep presapproval racial politicization representation dvElect gender sexism  	educ age white black latinx interest partyid ideology SCapproval  affective legitimacy{
	egen min = min(`var')
	egen max = max(`var')
	gen `var'2 = ((`var' - min)/(max-min))
	drop min max
}



* Means reported in Table 1
foreach var of varlist dvElect sexism racial representation legitimacy politicization ///
	SCapproval presapproval affective sophistication{
		mean `var'2
}


* Estimates in Figure 3 and Table A3
reg dvElect2  racial2  sexism2 representation2 gender2  ///
	educ2 age2 white2 black2 latinx2 sophistication2 partyid2 ideology2 ///
	SCapproval2  affective2  politicization2  presapproval2 legitimacy2
margins, at(legitimacy2 = (0(.1)1))
margins, at(racial2 = (0(.1)1))
margins, at(sexism2 = (0(.1)1))
margins, at(representation2 = (0(.1)1))

	
* Sophistication interactions. Marginal effects in Figure 4, 
	* estimates in Tables A4 & A5
reg dvElect2  c.racial2##c.sophistication2  sexism2 representation2 gender2  ///
	educ2 age2 white2 black2 latinx2   partyid2 ideology2 ///
	SCapproval2  affective2  politicization2  presapproval2 legitimacy2
margins, dydx(racial2) at(sophistication2=(0(.1)1))
marginsplot

	
reg dvElect2  c.racial2  c.sexism2##c.sophistication2 c.representation2 gender2  ///
	educ2 age2 white2 black2 latinx2   partyid2 ideology2 ///
	SCapproval2  affective2  politicization2  presapproval2 legitimacy2
margins, dydx(sexism2) at(sophistication2=(0(.1)1))
marginsplot

reg dvElect2  c.racial2  c.sexism2 c.representation2##c.sophistication2 gender2  ///
	educ2 age2 white2 black2 latinx2   partyid2 ideology2 ///
	SCapproval2  affective2  politicization2  presapproval2 legitimacy2
margins, dydx(representation2) at(sophistication2=(0(.1)1))
marginsplot

reg dvElect2  c.racial2##c.sophistication2  c.sexism2##c.sophistication2 c.representation2##c.sophistication2 gender2  ///
	educ2 age2 white2 black2 latinx2   partyid2 ideology2 ///
	SCapproval2  affective2  politicization2  presapproval2 legitimacy2



	

* Robustness to alternate specification. Info reported in manuscript
ssc install mrobust 
	

mrobust reg dvElect2  racial2  sexism2 representation2 gender2  ///
	educ2 age2 white2 black2 latinx2 sophistication2 partyid2 ideology2 ///
	SCapproval2  affective2  politicization2  presapproval2 legitimacy2 


mrobust reg dvElect2   sexism2 racial2 representation2 gender2  ///
	educ2 age2 white2 black2 latinx2 sophistication2 partyid2 ideology2 ///
	SCapproval2  affective2  politicization2  presapproval2 legitimacy2

mrobust reg dvElect2  representation2  racial2  sexism2 gender2  ///
	educ2 age2 white2 black2 latinx2 sophistication2 partyid2 ideology2 ///
	SCapproval2  affective2  politicization2  presapproval2 legitimacy2
	
	
**** ACB checks -- reported in footnote and discussion section

* variable for date completed to account for ACB confirmation
gen date = 0
replace date = 1 in 873/1000
 
ttest dvElect2, by(date)
ttest racial2, by(date)
ttest sexism2, by(date)
ttest representation2, by(date)

reg dvElect2  racial2  sexism2 representation2 gender2  ///
	educ2 age2 white2 black2 latinx2 sophistication2 partyid2 ideology2 ///
	SCapproval2  affective2  politicization2  presapproval2 legitimacy2 i.date
	
* testing linear effect
sort endtime
gen date2 = _n 
reg dvElect2  racial2  sexism2 representation2 gender2  ///
	educ2 age2 white2 black2 latinx2 sophistication2 partyid2 ideology2 ///
	SCapproval2  affective2  politicization2  presapproval2 legitimacy2 date2
	

******* saving data as old -- used in Figure 2
 
 keep sophistication2 rep2 presapproval2 racial2 politicization2 representation2 dvElect2 gender2 sexism2  	educ2 age2 white2 black2 latinx2 interest2 partyid2 ideology2 SCapproval2  affective2 legitimacy2

saveold "saved_old.dta", version(12)
